package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;


    @Override
    public OverviewVo getOverview() {
        // 查询redis数据库
        log.info("查询redis数据库");
        Object overview = redisTemplate.opsForValue().get("OVERVIEW_DATA");
        if (overview != null) {
            log.info("从redis数据库中获取数据");
            return (OverviewVo) overview;
        }

        // 如果redis数据库中没有数据，则从数据库中查询
        log.info("从数据库中查询");
        // 查询线索和商机的概览数据
        OverviewVo clueOverview = clueMapper.getOverview();

        // 将商机数据复制给线索数据
        OverviewVo businessOverview = businessMapper.getOverview();

        // 将商机数据复制给线索数据
        BeanUtils.copyProperties(clueOverview,businessOverview, "businessTotal", "businessWaitAllot", "businessWaitFollow", "businessFollowing", "businessFalse", "businessConvertCustomer");

        //缓存redis
        log.info("缓存redis");
        redisTemplate.opsForValue().set("overview",businessOverview,10, TimeUnit.MINUTES);
        return businessOverview;
    }
}
